package org.brisling.ethic.queryService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.StringUtil;
import org.brisling.common.util.WhereUtil;
import org.brisling.ethic.domain.DailyEvaluation;
import org.brisling.ethic.domain.EthicEvaluate;
import org.brisling.ethic.domain.VOEthicEvaluateSub;
import org.brisling.ethic.domain.VOEvalCount;
import org.brisling.ethic.util.EthicUtil;
import org.brisling.sysman.domain.SysLob;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.brisling.sysman.service.LobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/ethicAdmin/query")
public class QueryEthicAdminService extends AbstractBaseQueryService<EthicEvaluate>{
	
	
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;	
	
	
	@Autowired
	private QueryDictionaryService dicService;
	
	@Autowired
	private QueryDicSubjectService qryDicSubjectService;
	
	
	@Autowired
	private QueryDailyEvaluationService qryDailyEvaluationService;
	@Autowired
	private LobService lobService;
	
//	private String deptid_code = "depart_code";
	
	public QueryEthicAdminService(){
		this.moduleName = ConstSysParam.MODULE_ETHIC_ADMIN;
		this.subSystem = ConstSysParam.SUBSYSTEM_ETHIC;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(EthicEvaluate.class);
		this.clazz = EthicEvaluate.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap = new LinkedHashMap<String,String>();
		
		orderMap.put("bizno",null);		
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		
		return super.getBizMainPage();
		
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	
	//返回科室考评的编辑界面
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return super.getBizEditPage();
	}

	
	@RequestMapping("/getListDomain.action")
	public ListDomain<EthicEvaluate> getListDomain(@RequestBody(required=false) String body){		
		
		
		Map<String,Object> conditionMap = this.getBodyParam(body);
		Map<String,String> param_map = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
		
		param_map =param_map == null?new HashMap<String,String>():param_map;
		
		
		
		conditionMap.put(ConstSysParam.CONDITION_MAP_KEY, param_map);
		
		LinkedHashMap<String,String> oMap = new LinkedHashMap<String,String>();
		
		oMap.put("evaluate_year", "desc");
		oMap.put("status", null);
		oMap.put("dept_score", "desc");
		oMap.put("sel_score", "desc");
		oMap.put("person_code", null);
		
		
		return super.getListDomainByConditionAndOrder(null, conditionMap, typeMap, oMap, false);		
	}
	
	/**
	 * 获取科室考评人数统计信息
	 * @param body
	 * @return
	 */
	@RequestMapping("/getDeptCnt.action")
	public VOEvalCount getDeptCnt(@RequestBody(required=false) String body){
		VOEvalCount ret = new VOEvalCount();
		
		String bizcode = "qry_ethic_evaluate_count";
		
		String eval_year = EthicUtil.getEvaluateYear();
		String sql = dicService.getSqlByBizcode(bizcode);
		if(sql!=null){
			Map<String,Object> map = this.getBodyParam(body);
			
			if(map!=null && map.containsKey("condition")){
				Map<String,Object> condition = (Map<String,Object>) map.get("condition");
				condition.put("year", eval_year);
				sql = StringUtil.formatSql(sql,condition);
				return super.getOneBySql(sql,VOEvalCount.class);
			}
			
			return null;
		}
		return null;
		
	}
	
	@RequestMapping("/getList.action")
	public List<EthicEvaluate> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	public List<EthicEvaluate> getListAll(Map<String, String> map){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			return super.getAllList(sql);
		}
		return new ArrayList<EthicEvaluate>();		
		
	}
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public EthicEvaluate getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public EthicEvaluate getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<EthicEvaluate> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
	

	@RequestMapping("/getSubListDomainByMainId.action")
	public Object getSubListDomainByMainId(@RequestParam(required=false) Long lobid){		
		
		if(lobid!=null && lobid>0){			 
			SysLob domain = lobService.getOne(lobid);
			if(domain!=null && domain.getId()!=null){	
				String subContent = StringUtils.normalizeSpace(domain.getLob_contents());
				if(subContent != null){
					List<Object> sublist = JsonUtil.bodyToList(subContent, VOEthicEvaluateSub.class);
					return sublist;
				}
				
			}
		}
				
		return initSubList();
	}
	
	/**
	 * 查询获取加扣分项
	 * @param eval_year	考评年度
	 * @param uid		
	 * @return
	 */
	@RequestMapping("/getAddMinScore.action")
	public Map<String,Double> getAddMinScore(
				@RequestParam(value="uid",required=false)String uid,
				@RequestParam(value="eval_year",required=false)String eval_year){
		if(uid==null){
			return null;
		}
		if(eval_year==null){
			eval_year = Calendar.YEAR+"";
		}		
		
		List<DailyEvaluation> lists =qryDailyEvaluationService.getYearAndUserListAll(eval_year,uid);
		
		Double add_score = 0.0;
		Double min_score = 0.0;
		Map<String,Double> ret = new HashMap<String,Double>();
		if(lists!=null && lists.size()>0){
			for(DailyEvaluation de:lists){
				String _type = de.getAddorminus_type();
				if(_type!=null){
					_type = _type.trim();
					if(_type.indexOf("加分")>=0){
						add_score +=Double.parseDouble(de.getScore());
					}else{
						min_score +=Double.parseDouble(de.getScore());
					}
				}
			}
		}
		ret.put("add_score",add_score);
		ret.put("mark_score",min_score);
		return ret;
		
	}
	
	private ListDomain<Object> initSubList(){
		return qryDicSubjectService.getInitListDomain();
	}
	
	
}
